//
//  ZRHomeViewModel.swift
//  Sina
//
//  Created by 张锐 on 16/3/1.
//  Copyright © 2016年 zhangr. All rights reserved.
//
var count : Int = 0
import UIKit
import AFNetworking
class ZRHomeViewModel: NSObject {
    
    lazy var array:[ZRStatusModel] = [ZRStatusModel]()

    
    func loadData(since_id : Int,max_id : Int,finishClosure : ((i : Int)->())){
        
        let afn = AFHTTPSessionManager()
        
        let parameters = ["access_token":ZRTokenModelView.shareInstance.access_token ?? "","since_id" : since_id,"max_id" : max_id]
        
        afn.GET("https://api.weibo.com/2/statuses/home_timeline.json", parameters: parameters, progress: nil, success: { (_, JSON) -> Void in
            
            if let json = JSON as? [String : NSObject] {
                
                if let status = json["statuses"] as?[[String : NSObject]]{
                    
                    var i = 0
                    
                    for statu in status {
                        
                        let statusModel = ZRStatusModel(dic: statu)
                        
                        if since_id != 0 {
                        
                            self.array.insert(statusModel, atIndex: i)
                            
                            i++
                        
                        }else{
                        
                            self.array.append(statusModel)
                        }
                        
                        
//                        self.tableView.reloadData()
                        
                    }
                    finishClosure(i: i)
                    
                }
            }
            }) { (_, error) -> Void in
                print(error)
        }
    }

}
